package com.example.travel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.travel.domain.entity.Menu;
import com.example.travel.mapper.MenuMapper;
import com.example.travel.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author : LinQingLiang
 * @file : RoleServiceImpl.java
 * @Since : 2023/1/30 21:24
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;


    @Override
    public Page page(Menu menu, Integer pageNum, Integer pageSize) {
        Page page = new Page(pageNum,pageSize);

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like(menu.getName() != null && menu.getName() != "","name",menu.getName());
        return menuMapper.page(page,wrapper);
    }

    @Override
    public List<Menu> findMenus(String name) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper();
        queryWrapper.like(name!=null && name != "","name",name);
        queryWrapper.orderByAsc("id");
        List<Menu> menuList = menuMapper.selectList(queryWrapper);
        //找出pid为null的一级菜单
        List<Menu> parentNodes = menuList.stream().filter(menu -> menu.getPid() == null).collect(Collectors.toList());
        //找出一级菜单的子菜单
        for (Menu menu:parentNodes) {
            menu.setChildren(menuList.stream().filter(m -> menu.getId().equals(m.getPid())).collect(Collectors.toList()));
        }
        return parentNodes;
    }
}
